package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.StockDAO;
import com.fly.estore.domain.StockInfo;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/26.
 */
@Repository("StockDAO")
public class StockDAOImpl extends BaseDAO<StockInfo> implements StockDAO {

    public int addStock(final StockInfo stockInfo) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("insert into stock_info(stock_id, stock_type, create_id, create_time)")
                .append("values(?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, stockInfo.getStockId());
                ps.setString(i++, stockInfo.getStockType());
                ps.setString(i++, stockInfo.getCreateId());
                ps.setString(i++, stockInfo.getCreateTime());
            }
        });
        return update;
    }

    public int updateStock(final StockInfo stockInfo) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("update stock_info set")
                .append(" stock_type = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where stock_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, stockInfo.getStockType());
                ps.setString(i++, stockInfo.getLastUpdateId());
                ps.setString(i++, stockInfo.getLastUpdateTime());
                ps.setString(i++, stockInfo.getStockId());
            }
        });
        return update;
    }

    public int deleteById(final String stockId) {
        int update = 0;
        String sql = "delete from stock_info where stock_id = ?";
        update = getJdbcTemplate().update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, stockId);
            }
        });
        return update;
    }

    public StockInfo getById(String stockId) {
        StockInfo stock = new StockInfo();
        String sql = "select * from stock_info where stock_id = ?";
        List<StockInfo> resultList = getJdbcTemplate().query(sql, new StockRowMapper(), new Object[]{stockId} );
        if (!CollectionUtils.isEmpty(resultList)){
            stock = resultList.get(0);
        }
        return stock;
    }

    public List<StockInfo> getList() {
        List<StockInfo> stockList = new ArrayList<StockInfo>();
        String sql = "select * from stock_info order by create_time desc";
        stockList = getJdbcTemplate().query(sql, new StockRowMapper());
        return stockList;
    }

    public class  StockRowMapper implements RowMapper<StockInfo> {
        public StockInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            StockInfo stock = new StockInfo();
            stock.setStockId(rs.getString("stock_id"));
            stock.setStockType(rs.getString("stock_type"));
            stock.setCreateId(rs.getString("create_id"));
            stock.setCreateTime(rs.getString("create_time"));
            stock.setLastUpdateId(rs.getString("last_update_id"));
            stock.setLastUpdateTime(rs.getString("last_update_time"));
            return stock;
        }
    }
}
